package com.chinemit.cui.components.container.panel.titlewindow
{
	import com.chinemit.cui.components.bitmap.BitmapScale9Grid;
	import com.chinemit.cui.components.button.ImageButton;
	import com.chinemit.cui.components.button.ImageButton9Grid;
	import com.chinemit.cui.components.core.CUIComponent;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.FontStyle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.text.engine.TextElement;

	public class TitleWindow extends CUIComponent
	{
		private var windowWith:Number;
		private var windowHeight:Number;
		private var container:Sprite;
		private var containerMask:Sprite;
		
		private var backgournd:BitmapScale9Grid;
		
		public var btnClose:ImageButton;
		public var txtTitle:TextField;
		public var txtTitleHeight:TextField
		
		private var _title:String;
		/**
		 * 
		 * @param _w 背景宽度 
		 * @param _h 背景高度
		 * @param _cW 容器宽度
		 * @param _cH 容器高度
		 * @param _cX 容器X坐标
		 * @param _cY 容器Y坐标
		 * @param bg 背景图片
		 * 
		 */		
		public function TitleWindow(_til:String,
									_w:Number,
									_h:Number,
									_cLeft:Number,
									_cRight:Number,
									_cTop:Number,
									_cBottom:Number,
									_bg:BitmapScale9Grid = null
									
									)
		{
			super();
			
			title = _til;
			this.windowWith = _w;
			this.windowHeight = _h;
			
			if(_bg){
				backgournd = _bg;
				this.addChildAt(backgournd,0);
				backgournd.width= windowWith;
				backgournd.height = windowHeight;
			}else{
				trace("Error:Title window background is null!");
			}
			
			initContainer(_cLeft,_cRight,_cTop,_cBottom);
			
			
			if(btnClose){
				btnClose= new ImageButton(null,null,null);
				this.addChild(btnClose);
			}
			this.btnClose.addEventListener(MouseEvent.CLICK,closeEventHandler)
			this.btnClose.x = this.windowWith - this.btnClose.width*0.5;
			
			
			if(!txtTitle){
				txtTitle = new TextField;
				txtTitleHeight = new TextField
					
				this.addChild(txtTitle);
				this.addChild(txtTitleHeight);
			}
			
			this.txtTitle.width = this.txtTitleHeight.width = this.windowWith;
			this.setChildIndex(btnClose,this.numChildren-1);
			
		}
		
		public function addElement(arg0:DisplayObject):void{
			
			if(arg0){
				this.container.addChild(arg0);
			}
		}

		
		public function get title():String
		{
			return _title;
		}
		
		public function set title(value:String):void
		{
			if(value){
				_title = value;
			}else{
				_title = "";
			}
			txtTitle.text = txtTitleHeight.text = _title;
		}
		
		/**
		 * 关闭事件监听
		 * @param event
		 * 
		 */		
		private function closeEventHandler(event:MouseEvent):void{
			
			this.dispatchEvent(new Event(Event.CLOSE));
		}
		
		/**
		 * 
		 * @param left
		 * @param right
		 * @param top
		 * @param bottom
		 * 
		 */			
		private function initContainer(left:Number,right:Number,top:Number,bottom:Number):void{
			
			var w:Number = this.windowWith - left-right;
			var h:Number = this.windowHeight - top - bottom;
			
			//Draw container mask
			containerMask = new Sprite();
			containerMask.graphics.beginFill(0xffffff,1);
			containerMask.graphics.drawRect(left,top,w,h);
			containerMask.graphics.endFill();
			this.addChild(containerMask);
			
			container = new  Sprite();
			this.addChild(container);
			
			container.x = left;
			container.y = top;
			container.mask = containerMask;
			
		}
		 
	}
}